<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Users_ajax extends MY_Controller {

    function __construct(){

        parent::__construct();

        $this->load->model('users_model');

    }

    public function signup(){
	
        $output=array();

        $post=$this->input->post(null,true);

        foreach($post as $item=>$data){
                if($data==''){
                        $output=array(
                                'status'=>FORM_VALIDATION_ERROR,
                                'message'=>FORM_EMPTY_MESSAGE,
                        );
                        print json_encode($output);exit;
                        break;
                }
        }

        if(valid_email($post['email'])==false){
            $output=array(
                'status'=>FORM_VALIDATION_ERROR,
                'message'=>FORM_INVALID_EMAIL_MESSAGE,
            );
        }

        if($this->users_model->users_check_unique_mail($post['email'])==false){
            $output=array(
                'status'=>FORM_VALIDATION_ERROR,
                'message'=>FORM_ALREADY_TAKEN_EMAIL_MESSAGE,
            );
        }

        if($post['email']!=$post['re-email']){
            $output=array(
                'status'=>FORM_VALIDATION_ERROR,
                'message'=>FORM_NOT_MATCH_EMAIL_MESSAGE,
            );
        }

        if($post['password']!=$post['re-password']){
            $output=array(
                'status'=>FORM_VALIDATION_ERROR,
                'message'=>FORM_NOT_MATCH_PASS_MESSAGE,
            );
        }

        if(empty($output)){
            if($this->users_model->users_register_user($post)){
                $output=array(
                    'status'=>FORM_VALIDATION_SUCCEED,
                    'message'=>  base_url('home'),
                );
            }
        }

        print json_encode($output);exit;
    }
}